package org.timeismoney.payment.web.action;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.timeismoney.payment.common.PageBean;
import org.timeismoney.payment.model.ItemPaymentDetail;
import org.timeismoney.payment.model.ItemStuDetail;
import org.timeismoney.payment.service.ItemStuDetailService;
import org.timeismoney.payment.service.PaymentRecordService;
import org.timeismoney.payment.web.WebUtils;
import org.timeismoney.payment.web.form.ItemDetailForm;

public class Item4StuAction extends BaseAction {
	private ItemStuDetailService	stuDetailService;
	private PaymentRecordService	recordService;

	public void setRecordService(PaymentRecordService recordService) {
		this.recordService = recordService;
	}

	public void setStuDetailService(ItemStuDetailService stuDetailService) {
		this.stuDetailService = stuDetailService;
	}

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// rows=10&page=1
		// sidx schoolYearNo
		// sord desc
		String rows = request.getParameter("rows");
		String page = request.getParameter("page");
		String sortCol = request.getParameter("sidx");
		String sortD = request.getParameter("sord");
		int rowsNo = 10;
		try {
			rowsNo = Integer.valueOf(rows);
		} catch (RuntimeException e) {
		}
		int pageNo = 1;
		try {
			pageNo = Integer.valueOf(page);
		} catch (RuntimeException e) {
		}
		PageBean<ItemPaymentDetail> pageBean = new PageBean<ItemPaymentDetail>(
				pageNo, rowsNo, sortCol, sortD);
		ItemDetailForm detailForm = (ItemDetailForm) form;
		List<ItemPaymentDetail> items = stuDetailService.findItemPaymentByStu(
				detailForm.getStuNo(), detailForm.getSchoolYearNo());
		//find paid total amount
		BigDecimal paidAmt = recordService.getTotalPaySuccessByStuNoAndYearNo(detailForm.getStuNo(), detailForm.getSchoolYearNo());
		if(paidAmt!=null){
			ItemStuDetail finDetail = new ItemStuDetail();
			finDetail.setComment("已支付金额");
			finDetail.setCost(paidAmt.multiply(new BigDecimal(-1)));
			finDetail.setItemPaymentNo(-1);
			items.add(finDetail);
		}
		pageBean.setItems(items);
		pageBean.setTotalRows(items.size());
		renderJson(response, WebUtils.generateJSONByPageBean(pageBean));
		return null;
	}
}
